/*
*
==============================================================================
*  Name        : 
*  Part of     : Omen Game Engine - OpenGL Plugin
*  Description : 
*  Version     : 1.0
*
*  Copyright (c) 2013 Lauri Kortevaara.
* 
==============================================================================
*/
#include "DebugConsole.h"

#include <iostream>

#include <GL/GL.h>
#include <GL/GLU.h>
#include <GL/glut.h>


/***
** Some global parameters
*/
std::wstring debugLog;
int gYPos = 0.0;
int g_MaxYPos = 0.0;
double gYSpeed = 0.0;
double blink = 0.0;
bool g_bAutoScroll = true;

DebugConsole::DebugConsole(void)
{
    std::wstreambuf * old = std::wcout.rdbuf(m_debugOutBuf.rdbuf());
}

DebugConsole::~DebugConsole(void)
{
}

void DebugConsole::render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // Clear Screen And Depth Buffer
    glLoadIdentity();                                    // Reset The Current Modelview Matrix

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();

    glOrtho(0, m_window.width(), m_window.height(), 0.0, 0.0, 1.0 );

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    glDisable( GL_LIGHTING );
    glDisable( GL_TEXTURE_2D );

    // Render text and quads
    char text[20] = "Hello world!\0";
    float xPos = 0.0;
    float yPos = 10.0;
   
    gYSpeed *= 0.90; // Decrease the scroll speed;
   
    gYPos += gYSpeed/50.0;
    yPos = (double)10.0-gYPos;
    glRasterPos2d( xPos, yPos );
    std::wstring strDebug = m_debugOutBuf.str();
    
    glColor3f( 1.0, 1.0, 1.0 );
    for( std::wstring::iterator iter = strDebug.begin(); iter != strDebug.end(); ++iter )
    {
        char asciiChar = *iter;
        if( asciiChar == VK_RETURN || asciiChar == '\n' )
        {
            yPos += 10.0;
            xPos = 0.0;
            glRasterPos2d( xPos, yPos );
            continue;
        }
        if( asciiChar == VK_TAB )
        {
            for( int i=0; i < 4; ++i )glutBitmapCharacter(GLUT_BITMAP_8_BY_13, ' ' );
            continue;
        }
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, asciiChar );
    }

    // Write the users loggings
    glColor3f( 1.0, 1.0, 1.0 );
    
    for( std::wstring::iterator iter = debugLog.begin(); iter != debugLog.end(); ++iter )
    {
        char asciiChar = *iter;
        if( asciiChar == VK_RETURN )
        {
            yPos += 10.0;
            glRasterPos2d( xPos, yPos );
            continue;
        }
        if( asciiChar == VK_TAB )
        {
            xPos += 40.0;
            glRasterPos2d( xPos, yPos );
        }
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, asciiChar );
    }

     if( g_bAutoScroll && yPos > m_window.height() )
         if( gYSpeed < 5000.0 ) gYSpeed += 5000.0;
     
     if( g_bAutoScroll && (yPos-m_window.height()) < 0 )
         gYSpeed = -50.0;

    blink += 1.0;
    double alpha = fabs(sin( blink ));
    glColor3f( alpha, alpha, alpha );
    glutBitmapCharacter(GLUT_BITMAP_8_BY_13,'_');
    glColor3f( 1.0, 1.0, 1.0 );

    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
}